package cn.dylan.system.controller;

import cn.dylan.basic.util.JsonResult;
import cn.dylan.basic.util.PageList;
import cn.dylan.system.domain.SystemDictionaryType;
import cn.dylan.system.query.SystemDictionaryTypeQuery;
import cn.dylan.system.service.ISystemDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionaryType")
@Api(value = "员工的API",description="相关的CRUD功能")
public class SystemDictionaryTypeController {
    @Autowired
    private ISystemDictionaryTypeService systemDictionaryTypeService;

    /**
     *
     * @return
     */
    @ApiOperation(value = "查询所有" )
    @GetMapping
    public List<SystemDictionaryType> loadAll(){
        return systemDictionaryTypeService.loadAll();
    }

    /**
     * GetMapping 请求
     * @requstMapping(value = "", method = "get")
     * @param id
     * @return
     */
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public SystemDictionaryType loadById(@PathVariable("id") Long id){
        return systemDictionaryTypeService.LoadById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一个部门" )
    public JsonResult delete(@PathVariable("id") Long id){
        System.out.println(id);
        try {
            systemDictionaryTypeService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 修改和添加
     * @param systemDictionaryType
     * @return
     */
    @PostMapping
    @ApiOperation(value = "修改或者添加" )
    public JsonResult updateOrAdd(@RequestBody SystemDictionaryType systemDictionaryType){
        System.out.println(systemDictionaryType);
        try{
            if(systemDictionaryType.getId()==null){
                //添加操作
                systemDictionaryTypeService.save(systemDictionaryType);
            }else {
                systemDictionaryTypeService.update(systemDictionaryType);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PutMapping
    @ApiOperation(value = "分页查询" )
    public PageList<SystemDictionaryType> QueryPage(@RequestBody SystemDictionaryTypeQuery query){
        System.out.println(query);
        try {
            return systemDictionaryTypeService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList();
        }
    }

    /**
     * 批量删除
     */
    @ApiOperation(value = "批量删除部门" )
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            systemDictionaryTypeService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

}
